package exec;

import context.Context;
import utils.FileUtils;
import utils.FormatUtils;

import java.io.*;
import java.util.*;
import java.util.logging.Logger;

/**
 * 创建多个Facade.service.logic
 * Created by baolong on 2016/3/6.
 */
public class GeneratorDubboServiceAll {

    public static final String charset="utf-8";

    public static void main(String[] args) throws Exception {
        exec("E:\\work\\GFS_NEW_IntenlliJ\\SN\\branches\\SN-parent_0.0.1_BR", new ArrayList<String>() {
            {
                this.add("JkBus[金融系统业务];serviceA[类注释ServiceA];serviceAA[方法注释ServiceAA];logicAAA[逻辑注释logicAAA],logicAAB,logicAAC");
                this.add("JkBus[金融系统业务];serviceA[类注释ServiceA];serviceAB[方法注释ServiceAB];logicABA,logicABB");
                this.add("JkBus[金融系统业务];serviceA[类注释ServiceA];serviceAC[方法注释ServiceAC];logicACA,logicACC");
                this.add("JkBus[金融系统业务];serviceB[类注释ServiceB];serviceBA[方法注释ServiceBA];logicBAA,logicBAB,logicBAC");
                this.add("BkBus[金融系统业务];serviceB[类注释ServiceB];serviceBB[方法注释ServiceBB];logicBBA,logicBBB");
                this.add("BkBus[金融系统业务];serviceB[类注释ServiceB];serviceBC[方法注释ServiceBC];logicBCA,logicBCC");
            }
        });
    }




    public static void exec(String basePath,List<String> properties) throws Exception{
        Context c = new Context();
        c.put("facadeServicePath", basePath+"\\SN-dubbo\\src\\main\\java\\cn\\com\\gome\\sn\\dubbo");
        c.put("facadeServiceImplPath", basePath+"\\SN-service\\src\\main\\java\\cn\\com\\gome\\sn\\dubbo\\impl");
        c.put("servicePath",basePath+"\\SN-service\\src\\main\\java\\cn\\com\\gome\\sn\\service");
        c.put("serviceImplPath",basePath+"\\SN-service\\src\\main\\java\\cn\\com\\gome\\sn\\service\\Impl");
        c.put("logicImplPath",basePath+"\\SN-service\\src\\main\\java\\cn\\com\\gome\\sn\\logic\\impl");
        c.put("consumerPath",basePath+"\\SN-service\\src\\main\\resources\\spring\\");
        c.put("providerPath", basePath + "\\SN-service\\src\\main\\resources\\spring\\");
        c.put("facadeServicePackage", "cn.com.gome.sn.dubbo");
        c.put("facadeServiceImplPackage", "cn.com.gome.sn.dubbo.impl");
        c.put("servicePackage", "cn.com.gome.sn.service");
        c.put("serviceImplPackage", "cn.com.gome.sn.service.impl");
        c.put("logicImplPackage", "cn.com.gome.sn.logic.impl");


        //需要修改内容-------开始
        //每一条记录都是一个业务：唯一键为service_method
        for(String s : properties){
            c.addtoList("facade;service;method;logic...",s);
        }
        transFacadeServiceLogicTree(c);
        //逐个生成文件
        invokeOneByOne(c);
    }

    /**
     * facade;service;method;logic...
     * @param c
     * @throws Exception
     */
    public static void transFacadeServiceLogicTree(Context c) throws Exception {
        LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,List<String>>>> tree = new LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,List<String>>>>();

        LinkedHashMap<String,List<String>> serviceMethodMap = new LinkedHashMap<String, List<String>>();
        LinkedHashMap<String,List<String>> serviceLogicMap = new LinkedHashMap<String, List<String>>();
        LinkedHashMap<String,List<String>> serviceMethodLogicMap = new LinkedHashMap<String, List<String>>();

        List<String> facadeNameArray = new ArrayList<String>();
        List<String> serviceNameArray = new ArrayList<String>();
        List<String> logicNameArray = new ArrayList<String>();

        List<String> l = (List)c.get("facade;service;method;logic...");
        if(l==null || l.size()==0){
            return;
        }
        //将所有的facade放入tree
        for(String s :l){
            String facadeName = s.split(";")[0];
            if(tree.get(facadeName)==null){
                tree.put(facadeName,new LinkedHashMap<String, LinkedHashMap<String, List<String>>>());
            }
            if(!facadeNameArray.contains(facadeName)){
                facadeNameArray.add(facadeName);
            }
        }
        //将所有的service放入对应的tree上的facade
        for(String s :l){
            String facadeName = s.split(";")[0];
            String serviceName = s.split(";")[1];
            LinkedHashMap<String, LinkedHashMap<String, List<String>>> facade = tree.get(facadeName);
            if(facade.get(serviceName)==null){
                facade.put(serviceName,new  LinkedHashMap<String, List<String>>());
            }
            if(!serviceNameArray.contains(serviceName)){
                serviceNameArray.add(serviceName);
            }
        }
        //将所有的method放入对应的tree上的facade上的service上
        for(String s :l){
            String facadeName = s.split(";")[0];
            String serviceName = s.split(";")[1];
            String methodName = s.split(";")[2];
            LinkedHashMap<String, LinkedHashMap<String, List<String>>> facade = tree.get(facadeName);
            LinkedHashMap<String, List<String>> service = facade.get(serviceName);
            if(service.get(methodName)==null){
                service.put(methodName, new ArrayList<String>());
            }
            //设置service和method的关系
            List<String> serviceMethod = serviceMethodMap.get(serviceName);
            if(serviceMethod==null){
                serviceMethod = new ArrayList<String>();
                serviceMethodMap.put(serviceName,serviceMethod);
            }
            if(!serviceMethod.contains(methodName)){
                serviceMethod.add(methodName);
            }
        }
        //将所有的logic放入对应的tree上的facade上的service上的method上
        for(String s :l){
            String facadeName = s.split(";")[0];
            String serviceName = s.split(";")[1];
            String methodName = s.split(";")[2];
            String logicStr = s.split(";")[3];
            LinkedHashMap<String, LinkedHashMap<String, List<String>>> facade = tree.get(facadeName);
            LinkedHashMap<String, List<String>> service = facade.get(serviceName);
            List<String> methodsLogic = service.get(methodName);
            List<String> logic_of_service = serviceLogicMap.get(serviceName);
            if(logic_of_service==null){
                logic_of_service = new ArrayList<String>();
                serviceLogicMap.put(serviceName,logic_of_service);
            }
            for(String logic:logicStr.split(",")){
                if(!methodsLogic.contains(logic)){
                    methodsLogic.add(logic);
                }
                if(!logicNameArray.contains(logic)){
                    logicNameArray.add(logic);//在全量中添加
                }
                if(!logic_of_service.contains(logic)){
                    logic_of_service.add(logic);//在当前的serviceLogicMap中添加
                }

            }
            //设置serviceMethod和logic的关系
            List<String> serviceMethodLogic = serviceMethodLogicMap.get(serviceName+methodName);
            if(serviceMethodLogic==null){
                serviceMethodLogic = new ArrayList<String>();
                serviceMethodLogicMap.put(serviceName+methodName,serviceMethodLogic);
            }
            if(!serviceMethodLogic.contains(methodName)){
                serviceMethodLogic.addAll(methodsLogic);
            }

        }
        c.put("facadeNameArray", facadeNameArray);//facade名称列表-全量
        c.put("serviceNameArray", serviceNameArray);//service名称列表-全量
        c.put("logicNameArray", logicNameArray);//logic名称列表-全量
        c.put("FacadeServiceMethodLogicTree", tree);//整体树形结构
        c.put("ServiceMethodMap", serviceMethodMap);//service和method额映射关系
        c.put("ServiceLogicMap", serviceLogicMap);//service和logic额映射关系
        c.put("ServiceMethodLogicMap",serviceMethodLogicMap);//Servicemethod和logic的映射关系
    }

    /**
     * 一个一个的执行
     */
    public static void invokeOneByOne(Context c) throws Exception {
        LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,List<String>>>> tree = getFacadeServiceMethodLogicTree(c);
        if(tree==null || tree.size()==0){
            return;
        }
        //需要修改内容-------结束
        for(String facadeName : tree.keySet()){
            c.put("currentFacadeName",facadeName);
            //------------------创建FacadeService和Impl--------------------------------------------------------
            createfacadeService(c);
            createfacadeImplService(c);

            //------------------创建service和serviceImpl--------------------------------------------------------
            List<String> serviceNameArray = (List<String>)c.get("serviceNameArray");
            if(serviceNameArray!=null){
                for(String serviceName : serviceNameArray){
                    c.put("currentServiceName",serviceName);
                    createService(c);
                    createServiceImpl(c);
                }
            }

            //------------------创建每一个logic--------------------------------------------------------
            List<String> logicNameArray = (List<String>)c.get("logicNameArray");
            if(logicNameArray!=null&&logicNameArray.size()>0){
                for(String logicName : logicNameArray){
                    c.put("currentLogicName", logicName);
                    createLogic(c);
                }
            }
            //------------------生成facade的xml文件--------------------------------------------------------
            createConsumerXml(c);
            createProviderXml(c);
        }


    }


    public static String getRealName(String name){
        if(name.indexOf("[")>-1){
            return name.split("\\[")[0];
        }else{
            return name;
        }
    }

    public static String getRealDesc(String desc){
        if(desc.indexOf("[")>-1){
            return desc.split("\\[")[1].replaceAll("]","");
        }else{
            return "";
        }
    }
    public static  LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,List<String>>>> getFacadeServiceMethodLogicTree(Context c){
        return (LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,List<String>>>>) c.get("FacadeServiceMethodLogicTree");
    }

    /**
     * 创建facadeService
     * @param c
     */
    public static void createfacadeService(Context c) throws Exception {
        //创建文件夹
        String currentFacadeName = (String)c.get("currentFacadeName");
        String facadeServicePath = (String)c.get("facadeServicePath");
        String facadeServicePackage = (String)c.get("facadeServicePackage");
        if(!FileUtils.mkdirs(facadeServicePath)){
            throw new Exception("文件夹["+facadeServicePath+"]创建不成功");
        }
        String className = FormatUtils.firstBig(getRealName(currentFacadeName))+"FacadeService";
        String fileName = className+".java";
        File entityFile = new File(facadeServicePath + File.separator+fileName);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(entityFile),charset));

        //准备内容
        StringBuilder sb = new StringBuilder();
        sb.append("/**\r\n * Gome SN Generator \r\n */\r\n");
        sb.append("package " + facadeServicePackage +";").append("\r\n").append("\r\n");
        sb.append("import cn.com.gome.sn.dubbo.bean.SN_ParametersVo;").append("\r\n").append("\r\n").append("\r\n");

        //增加类注释
        sb.append("/**").append("\r\n");
        sb.append(" * " + getRealDesc(currentFacadeName)).append("\r\n");
        sb.append(" */").append("\r\n").append("\r\n");

        sb.append("public interface " + className + " {").append("\r\n");
        sb.append("").append("\r\n");
        sb.append("    /**").append("\r\n");
        sb.append("     * @Title: service").append("\r\n");
        sb.append("     * @Description: dubbo服务").append("\r\n");
        sb.append("     * @param vo").append("\r\n");
        sb.append("     * @return SN_ParametersVo").append("\r\n");
        sb.append("     * @throws").append("\r\n");
        sb.append("     */").append("\r\n");
        sb.append("     public SN_ParametersVo service(SN_ParametersVo vo);").append("\r\n").append("\r\n");
        sb.append("}").append("\r\n");

        //增加标签
        pw.println(sb.toString());
        pw.close();
    }

    /**
     *
     * @param c
     * @throws Exception
     */
    private static void createfacadeImplService(Context c) throws Exception {
        //创建文件夹
        String currentFacadeName = (String)c.get("currentFacadeName");
        String businessName = getRealName(currentFacadeName);
        String businessDesc = getRealDesc(currentFacadeName);
        String facadeServiceImplPath = (String)c.get("facadeServiceImplPath");
        String facadeServiceImplPackage = (String)c.get("facadeServiceImplPackage");
        if(!FileUtils.mkdirs(facadeServiceImplPath)){
            throw new Exception("文件夹["+facadeServiceImplPath+"]创建不成功");
        }
        String className = FormatUtils.firstBig(businessName)+"FacadeServiceImpl";
        String fileName = className+".java";
        File entityFile = new File(facadeServiceImplPath + File.separator+fileName);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(entityFile),charset));

        //准备内容
        StringBuilder sb = new StringBuilder();
        sb.append("/**\r\n * Gome SN Generator \r\n */\r\n");
        sb.append("package " + facadeServiceImplPackage +";").append("\r\n").append("\r\n");
        sb.append("import cn.com.gome.frame.logger.HLogger;").append("\r\n");
        sb.append("import cn.com.gome.frame.logger.LoggerManager;").append("\r\n");
        sb.append("import cn.com.gome.frame.util.StringUtils;").append("\r\n");
        sb.append("import cn.com.gome.sn.dubbo."+FormatUtils.firstBig(businessName)+"FacadeService;").append("\r\n");
        sb.append("import cn.com.gome.sn.dubbo.bean.SN_ParametersVo;").append("\r\n");
        sb.append("").append("\r\n");
        LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,List<String>>>> tree = getFacadeServiceMethodLogicTree(c);
        Set<String> serviceSet = tree.get(currentFacadeName).keySet();
        if(serviceSet!=null){
            for(String serviceName:serviceSet) {
                sb.append("import cn.com.gome.sn.service."+FormatUtils.firstBig(getRealName(serviceName))+"Service;").append("\r\n");
            }
        }
        sb.append("").append("\r\n");
        sb.append("import org.springframework.beans.factory.annotation.Autowired;").append("\r\n");
        sb.append("import org.springframework.stereotype.Service;").append("\r\n");
        sb.append("").append("\r\n");
        sb.append("@Service").append("\r\n");
        sb.append("public class " + className + " implements " + FormatUtils.firstBig(businessName) + "FacadeService {").append("\r\n");
        sb.append("    protected HLogger logger = LoggerManager.getLoger(\""+className+"\");").append("\r\n").append("\r\n");


        if(serviceSet!=null){
            for(String serviceName:serviceSet) {
                sb.append("    @Autowired").append("\r\n");
                sb.append("    private " + FormatUtils.firstBig(getRealName(serviceName)) + "Service " + FormatUtils.firstSmall(getRealName(serviceName)) + "Service;").append("\r\n").append("\r\n");
            }
        }




        sb.append("").append("\r\n");
        sb.append("    public SN_ParametersVo service(SN_ParametersVo vo) {").append("\r\n");
        sb.append("        logger.info(\""+className+".service--start\");").append("\r\n");
        sb.append("        String sources = vo.getSources();").append("\r\n");
        sb.append("        String version = vo.getVersion();").append("\r\n");
        sb.append("        String businessType = vo.getBusinessType();").append("\r\n");
        sb.append("        if (StringUtils.isEmpty(sources)||StringUtils.isEmpty(version)||StringUtils.isEmpty(businessType)) {").append("\r\n");
        sb.append("            vo.setResCode(\"0002\");").append("\r\n");
        sb.append("            vo.setResDesc(\"source/version/businessType不可以为空\");").append("\r\n");
        sb.append("            return vo;").append("\r\n");
        sb.append("        }").append("\r\n").append("\r\n");



        if(serviceSet!=null){
            int count =0;
            for(String serviceName:serviceSet){
                Set<String> methodSet = tree.get(currentFacadeName).get(serviceName).keySet();
                for(String methodName : methodSet){
                    if(count == 0){
                        sb.append("        if(\"" + getRealName(serviceName).toUpperCase()+"_"+ getRealName(methodName).toUpperCase() + "\".equals(businessType)) {").append("\r\n");
                        sb.append("            " + FormatUtils.firstSmall(getRealName(serviceName)) + "Service." + FormatUtils.firstSmall(getRealName(methodName)) + "(vo);").append("\r\n");
                    }else{
                        sb.append("        } else if(\""+getRealName(serviceName).toUpperCase()+"_"+ getRealName(methodName).toUpperCase() +"\".equals(businessType)) {").append("\r\n");
                        sb.append("            " + FormatUtils.firstSmall(getRealName(serviceName)) + "Service." + FormatUtils.firstSmall(getRealName(methodName)) + "(vo);").append("\r\n");
                    }
                    count++;
                }
            }
            //最后一个
            sb.append("        } else {").append("\r\n");
            sb.append("            vo.setResCode(\"0002\");").append("\r\n");
            sb.append("            vo.setResDesc(\"没有找到指定的BUSINESSTYPE\");").append("\r\n");
            sb.append("        }").append("\r\n");
        }

        sb.append("        logger.info(\""+className+".service--end\");").append("\r\n");
        sb.append("        return vo;").append("\r\n");
        sb.append("").append("\r\n");
        sb.append("    }").append("\r\n");
        sb.append("}").append("\r\n");

        //增加标签
        pw.println(sb.toString());
        pw.close();
    }

    public static void createService(Context c) throws Exception {
        //创建文件夹
        String servicePath = (String)c.get("servicePath");
        String servicePackage = (String)c.get("servicePackage");
        String currentServiceName = (String)c.get("currentServiceName");
        if(!FileUtils.mkdirs(servicePath)){
            throw new Exception("文件夹["+servicePath+"]创建不成功");
        }
        String className = FormatUtils.firstBig(getRealName(currentServiceName))+"Service";
        String fileName = className+".java";
        File entityFile = new File(servicePath + File.separator+fileName);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(entityFile),charset));

        //准备内容
        StringBuilder sb = new StringBuilder();
        sb.append("/**\r\n * Gome SN Generator \r\n */\r\n");
        sb.append("package " + servicePackage +";").append("\r\n").append("\r\n");
        sb.append("import cn.com.gome.sn.dubbo.bean.SN_ParametersVo;").append("\r\n");
        sb.append("").append("\r\n");
        sb.append("/**").append("\r\n");
        sb.append(" * " + getRealDesc(currentServiceName)).append("\r\n");
        sb.append(" */").append("\r\n");
        sb.append("public interface " + className + " {").append("\r\n");

        //遍历该Service的method清单
        LinkedHashMap<String,List<String>> serviceMethodMap = (LinkedHashMap<String,List<String>>)c.get("ServiceMethodMap");
        List<String> method_of_service = serviceMethodMap.get(currentServiceName);
        for(String methodName : method_of_service){
            sb.append("    /**").append("\r\n");
            sb.append("     * "+getRealDesc(methodName)).append("\r\n");
            sb.append("     */").append("\r\n");
            sb.append("    public SN_ParametersVo " + FormatUtils.firstSmall(getRealName(methodName))+"(SN_ParametersVo vo);").append("\r\n").append("\r\n");
        }
        sb.append("}").append("\r\n");
        //增加标签
        pw.println(sb.toString());
        pw.close();
    }



    public static void createServiceImpl(Context c) throws Exception {
        //创建文件夹
        String serviceImplPath = (String)c.get("serviceImplPath");
        String servicePackage = (String)c.get("servicePackage");
        String serviceImplPackage = (String)c.get("serviceImplPackage");
        String currentServiceName = (String)c.get("currentServiceName");
        if(!FileUtils.mkdirs(serviceImplPath)){
            throw new Exception("文件夹["+serviceImplPath+"]创建不成功");
        }
        String className = FormatUtils.firstBig(getRealName(currentServiceName))+"ServiceImpl";
        String fileName = className+".java";
        File entityFile = new File(serviceImplPath + File.separator+fileName);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(entityFile), charset));

        //准备内容
        StringBuilder sb = new StringBuilder();
        sb.append("/**\r\n * Gome SN Generator \r\n */\r\n");
        sb.append("package " + serviceImplPackage +";").append("\r\n").append("\r\n");
        sb.append("import javax.annotation.Resource;").append("\r\n");
        sb.append("import cn.com.gome.frame.logger.HLogger;").append("\r\n");
        sb.append("import cn.com.gome.frame.logger.LoggerManager;").append("\r\n");
        sb.append("import cn.com.gome.frame.logic.ILogics;").append("\r\n");
        sb.append("import cn.com.gome.frame.logic.ResultEnum;").append("\r\n");
        sb.append("import cn.com.gome.frame.exception.GomeFinanceException;").append("\r\n");
        sb.append("import cn.com.gome.sn.dubbo.bean.SN_ParametersVo;").append("\r\n");
        sb.append("import org.springframework.beans.factory.annotation.Autowired;").append("\r\n");
        sb.append("import org.springframework.stereotype.Service;").append("\r\n");
        sb.append("import " + servicePackage + "." + FormatUtils.firstBig(getRealName(currentServiceName)) + "Service;").append("\r\n");
        sb.append("").append("\r\n");
        sb.append("/**").append("\r\n");
        sb.append(" * " + getRealDesc(currentServiceName)).append("\r\n");
        sb.append(" */").append("\r\n");
        sb.append("@Service").append("\r\n");
        sb.append("public class " + className + " implements "+FormatUtils.firstBig(getRealName(currentServiceName))+"Service {").append("\r\n");
        sb.append("    private HLogger logger = LoggerManager.getLoger(\"" + className + "\");").append("\r\n");


        //遍历属于该service的每个method的每一个logic
        LinkedHashMap<String,LinkedHashMap<String,LinkedHashMap<String,List<String>>>> tree = getFacadeServiceMethodLogicTree(c);
        LinkedHashMap<String,List<String>> serviceMethodLogicMap = (LinkedHashMap<String,List<String>>)c.get("ServiceMethodLogicMap");
        LinkedHashMap<String,List<String>> serviceLogicMap = (LinkedHashMap<String,List<String>>)c.get("ServiceLogicMap");
        List<String> logic_of_service = serviceLogicMap.get(currentServiceName);

        for(String logicName:logic_of_service){
            sb.append("    /** "+getRealDesc(logicName)+" */").append("\r\n");
            sb.append("    @Resource").append("\r\n");
            sb.append("    public ILogics "+getRealName(logicName)+"Logic;").append("\r\n").append("\r\n");
        }

        sb.append("    //示例").append("\r\n");
        sb.append("    //public SN_ParametersVo " + FormatUtils.firstSmall(currentServiceName) + "(SN_ParametersVo vo) {").append("\r\n");
        sb.append("    //    logger.info(\"" + className + "." + FormatUtils.firstSmall(currentServiceName) + "--start\");").append("\r\n");
        sb.append("    //    try {").append("\r\n");
        sb.append("    //        if(logicName.exec(vo)!=ResultEnum.PartOK){").append("\r\n");
        sb.append("    //            return vo;").append("\r\n");
        sb.append("    //        }").append("\r\n");
        sb.append("    //    } catch (GomeFinanceException e) {").append("\r\n");
        sb.append("    //        vo.setResCode(\"0002\");").append("\r\n");
        sb.append("    //        vo.setResDesc(\"发生系统错误\");").append("\r\n");
        sb.append("    //        logger.error(\"发生系统错误\",e);").append("\r\n");
        sb.append("    //    }").append("\r\n");
        sb.append("    //    logger.info(\"" + className + "." + FormatUtils.firstSmall(currentServiceName) + "--end\");").append("\r\n");
        sb.append("    //    return vo;").append("\r\n");
        sb.append("    //}").append("\r\n").append("\r\n");
        //对每个方法进行遍历
        //遍历属于该service的每个method的每一个logic

        //遍历该Service的method清单
        LinkedHashMap<String,List<String>> serviceMethodMap = (LinkedHashMap<String,List<String>>)c.get("ServiceMethodMap");
        List<String> method_of_service = serviceMethodMap.get(currentServiceName);
        for(String methodName : method_of_service){
            sb.append("    public SN_ParametersVo " + FormatUtils.firstSmall(getRealName(methodName)) + "(SN_ParametersVo vo) {").append("\r\n");
            sb.append("        logger.info(\"" + className + "." + FormatUtils.firstSmall(getRealName(methodName)) + "--start\");").append("\r\n");
            sb.append("        try {").append("\r\n");
            List<String> logics =  serviceMethodLogicMap.get(currentServiceName+methodName);
            for(String logicName : logics){
                sb.append("            if(" + getRealName(logicName) + "Logic.exec(vo)!=ResultEnum.PartOK){").append("\r\n");
                sb.append("                return vo;").append("\r\n");
                sb.append("            }").append("\r\n");
            }

            sb.append("        } catch (GomeFinanceException e) {").append("\r\n");
            sb.append("            vo.setResCode(\"0002\");").append("\r\n");
            sb.append("            vo.setResDesc(\"发生系统错误\");").append("\r\n");
            sb.append("            logger.error(\"发生系统错误\",e);").append("\r\n");
            sb.append("        }").append("\r\n");
            sb.append("        logger.info(\"" + className + "." + FormatUtils.firstSmall(getRealName(methodName)) + "--end\");").append("\r\n");
            sb.append("        return vo;").append("\r\n");
            sb.append("    }").append("\r\n").append("\r\n");
        }
        sb.append("}").append("\r\n");
        //增加标签
        pw.println(sb.toString());
        pw.close();
    }

    public static void createLogic(Context c) throws Exception {
        //创建文件夹
        String logicImplPath = (String)c.get("logicImplPath");
        String logicImplPackage = (String)c.get("logicImplPackage");
        String currentLogicName = (String)c.get("currentLogicName");
        if(!FileUtils.mkdirs(logicImplPath)){
            throw new Exception("文件夹["+logicImplPath+"]创建不成功");
        }
        String className = FormatUtils.firstBig(getRealName(currentLogicName))+"Logic";
        String fileName = className+".java";
        File entityFile = new File(logicImplPath + File.separator+fileName);
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(entityFile),charset));

        //准备内容
        StringBuilder sb = new StringBuilder();
        sb.append("/**\r\n * Gome SN Generator \r\n */\r\n");
        sb.append("package " + logicImplPackage +";").append("\r\n").append("\r\n");
        sb.append("import javax.annotation.Resource;").append("\r\n");
        sb.append("import cn.com.gome.frame.logger.HLogger;").append("\r\n");
        sb.append("import cn.com.gome.frame.logger.LoggerManager;").append("\r\n");
        sb.append("import cn.com.gome.frame.logic.ILogics;").append("\r\n");
        sb.append("import cn.com.gome.frame.logic.ResultEnum;").append("\r\n");
        sb.append("import cn.com.gome.sn.dubbo.bean.SN_ParametersVo;").append("\r\n");
        sb.append("import cn.com.gome.frame.exception.GomeFinanceException;").append("\r\n");
        sb.append("import org.springframework.beans.factory.annotation.Autowired;").append("\r\n");
        sb.append("import org.springframework.transaction.annotation.Propagation;").append("\r\n");
        sb.append("import org.springframework.transaction.annotation.Transactional;").append("\r\n");
        sb.append("import org.springframework.beans.factory.annotation.Autowired;").append("\r\n");
        sb.append("import org.springframework.stereotype.Service;").append("\r\n");
        sb.append("").append("\r\n");
        sb.append("@Service").append("\r\n");
        sb.append("@Transactional").append("\r\n");
        sb.append("public class " + className + " implements ILogics<SN_ParametersVo> {").append("\r\n");
        sb.append("    private HLogger logger = LoggerManager.getLoger(\"" + className+"\");").append("\r\n");
        sb.append("    //@Autowired").append("\r\n");
        sb.append("    //public *Mapper *Mapper;").append("\r\n").append("\r\n");
        sb.append("").append("\r\n").append("\r\n");
        sb.append("    //示例").append("\r\n");
        sb.append("    //public ResultEnum exec(SN_ParametersVo vo) throws GomeFinanceException {").append("\r\n");
        sb.append("    //    logger.info(\""+className+".exec--start\");").append("\r\n");
        sb.append("    //    if(StringUtils.isEmpty(vo.getString(\"demoId\"))){").append("\r\n");
        sb.append("    //        vo.setResCode(\"0002\");").append("\r\n");
        sb.append("    //        vo.setResDesc(\"查询权限时demoId为空\");").append("\r\n");
        sb.append("    //        return ResultEnum.PartCase01;").append("\r\n");
        sb.append("    //    }").append("\r\n");
        sb.append("    //    List<TblDemoInfo>  menuList = new ArrayList<TblDemoInfo>();").append("\r\n");
        sb.append("    //    demoList =  tblDemoInfoMapper.queryList(demoId);").append("\r\n");
        sb.append("    //    vo.setResCode(\"0001\");").append("\r\n");
        sb.append("    //    vo.setResCode(\"查询完成\");").append("\r\n");
        sb.append("    //    vo.setInfo(\"result\",demoList);").append("\r\n");
        sb.append("    //    logger.info(\""+className+".exec--end\");").append("\r\n");
        sb.append("    //    return ResultEnum.OK;").append("\r\n");
        sb.append("    //}").append("\r\n");
        sb.append("    ").append("\r\n");
        sb.append("").append("\r\n");
        sb.append("    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)").append("\r\n");
        sb.append("    public ResultEnum exec(SN_ParametersVo vo) throws GomeFinanceException {").append("\r\n");
        sb.append("        logger.info(\""+className+".exec--start\");").append("\r\n");
        sb.append("        logger.info(\""+className+".exec--end\");").append("\r\n");
        sb.append("        return ResultEnum.PartOK;").append("\r\n");
        sb.append("    }").append("\r\n");
        sb.append("}").append("\r\n");
        //增加标签
        pw.println(sb.toString());
        pw.close();
    }

    /**
     * 创建ConsumerXml
     * @param c
     */
    public static void createConsumerXml(Context c) throws Exception {
        try{
            //创建文件夹
            String currentFacadeName = (String)c.get("currentFacadeName");
            String businessName = getRealName(currentFacadeName);
            String businessDesc = getRealDesc(currentFacadeName);
            String facadeServicePackage = (String)c.get("facadeServicePackage");
            String consumerPath = (String)c.get("consumerPath");
            String providerPath = (String)c.get("providerPath");
            if(!FileUtils.mkdirs(consumerPath)){
                throw new Exception("文件夹["+consumerPath+"]创建不成功");
            }
            String className = FormatUtils.firstBig(businessName)+"FacadeService";
            String fileName = "sn-consumer.xml";
            String interfaceName = facadeServicePackage+"."+className;
            File entityFile = new File(consumerPath + File.separator+fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(entityFile),charset));
            StringBuilder sb = new StringBuilder();
            String tmp=null;
            while((tmp=br.readLine())!=null){
                sb.append(tmp).append("\r\n");
            }
            br.close();
            if(sb.toString().indexOf(className)>-1){
                //如果已经发现了这个服务存在
                System.out.println("生成 sn-consumer.xml 时发现["+className+"]已经存在");
                return;
            }
            tmp = sb.toString().trim().replace("</beans>", "");//清除末尾的</beans>符号
            String newConsumer = "    <dubbo:reference timeout=\"1000000\" group=\"${dubbo.group}\" id=\""+FormatUtils.firstSmall(className)+"Impl\" interface=\""+interfaceName+"\" registry=\"SN-DubboProvider\" retries=\"0\" check=\"false\" />";
            tmp=tmp+"\r\n"+newConsumer;
            tmp = tmp+"\r\n</beans>";

            PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(entityFile),charset));
            //增加标签
            pw.println(tmp);
            pw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 创建ProviderXml
     * @param c
     */
    public static void createProviderXml(Context c) throws Exception {
        try{
            //创建文件夹
            String currentFacadeName = (String)c.get("currentFacadeName");
            String businessName = getRealName(currentFacadeName);
            String businessDesc = getRealDesc(currentFacadeName);
            String facadeServicePackage = (String)c.get("facadeServicePackage");
            String consumerPath = (String)c.get("consumerPath");
            String providerPath = (String)c.get("providerPath");
            if(!FileUtils.mkdirs(consumerPath)){
                throw new Exception("文件夹["+consumerPath+"]创建不成功");
            }
            String className = FormatUtils.firstBig(businessName)+"FacadeService";
            String fileName = "sn-provider.xml";
            String interfaceName = facadeServicePackage+"."+className;
            File entityFile = new File(consumerPath + File.separator+fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(entityFile),charset));
            StringBuilder sb = new StringBuilder();
            String tmp=null;
            while((tmp=br.readLine())!=null){
                sb.append(tmp).append("\r\n");
            }
            br.close();
            if(sb.toString().indexOf(className)>-1){
                //如果已经发现了这个服务存在
                System.out.println("生成 sn-consumer.xml 时发现["+className+"]已经存在");
                return;
            }
            tmp = sb.toString().trim().replace("</beans>", "");//清除末尾的</beans>符号
            String newConsumer = "    <dubbo:service loadbalance=\"${dubbo.loadbalance}\" timeout=\"${dubbo.run.timeout}\" group=\"${dubbo.group}\" interface=\""+interfaceName+"\" ref=\""+FormatUtils.firstSmall(className)+"Impl\" registry=\"SN-DubboProvider\"/>";
            tmp=tmp+"\r\n"+newConsumer;
            tmp = tmp+"\r\n</beans>";

            PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(entityFile),charset));
            //增加标签
            pw.println(tmp);
            pw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
